/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.webservicecomponent.metadata.serializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.component.schema.IGspCompResource;
import com.inspur.edp.component.schema.resource.IApiResource;
import com.inspur.edp.component.schema.resource.RpcResource;
import com.inspur.edp.webservicecomponent.metadata.WebServiceComponent;
import com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.WSComponent;
import com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.serializer.WSCmpSerializer;
import com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.sourceinfo.SourceInfo;
import com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.sourceinfo.impl.CafIApiSourceInfo;
import com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.sourceinfo.impl.SgIApiSourceInfo;
import com.inspur.edp.webservicecomponent.metadata.util.CompareUtil;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author Kaixuan Shi
 * @version 0.1
 */
public class SerializeTest {
    @Test
    public void oldDataSerializeTest() throws IOException {
        File fileDirectory = new File("./src/test/data");
        WebServiceCmpSerializer serializer = new WebServiceCmpSerializer();
        WSCmpSerializer oldSerializer = new WSCmpSerializer();
        for (File file : fileDirectory.listFiles()) {
            String componentStr = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
            JsonNode metadataNode = (new ObjectMapper()).readTree(componentStr);
            JsonNode compNode = JsonUtil.getPropValue(metadataNode, "Content", JsonNode.class);

            WebServiceComponent newComponent = (WebServiceComponent) serializer.DeSerialize(compNode);
            WSComponent oldComponent = (WSComponent) oldSerializer.DeSerialize(compNode);
            Assert.assertEquals(oldComponent.getId(), newComponent.getId());
            Assert.assertEquals(oldComponent.getCode(), newComponent.getCode());
            Assert.assertEquals(oldComponent.getName(), newComponent.getName());
            Assert.assertEquals(oldComponent.getApplication(), newComponent.getApplication());
            Assert.assertEquals(oldComponent.getServiceUnit(), newComponent.getServiceUnit());
            Assert.assertEquals(oldComponent.getDescription(), newComponent.getDescription());
            Assert.assertEquals(oldComponent.getVersion(), newComponent.getVersion());

            Assert.assertTrue(CompareUtil.differentObjectEquals(oldComponent.getRefStructures(), newComponent.getRefStructures()));
            Assert.assertTrue(CompareUtil.differentObjectEquals(oldComponent.getOperations(), newComponent.getOperations()));
            Assert.assertTrue(this.compareSourceInfo(oldComponent.getSourceInfo(), newComponent.getResource()));

            JsonNode cmpNode1 = serializer.Serialize(newComponent);
            WebServiceComponent newComponent2 = (WebServiceComponent) serializer.DeSerialize(cmpNode1);
            JsonNode cmpNode2 = serializer.Serialize(newComponent2);
            Assert.assertEquals(cmpNode1, cmpNode2);
        }

    }

    private boolean compareSourceInfo(SourceInfo sourceInfo, IGspCompResource resource) {
        if (sourceInfo == null && resource == null) {
            return true;
        }
        if ((sourceInfo == null && resource != null) || (sourceInfo != null && resource == null)) {
            return false;
        }

        if (sourceInfo.getSourceType() == CafIApiSourceInfo.SOURCE_TYPE) {
            if (resource.getKind() != RpcResource.KIND) {
                return false;
            }
            CafIApiSourceInfo cafInfo = (CafIApiSourceInfo) sourceInfo;
            RpcResource rpcResource = (RpcResource) resource;
            Assert.assertEquals(cafInfo.getApiVersion(), rpcResource.getApiVersion());
            Assert.assertEquals(cafInfo.getCommonApi(), rpcResource.isCommonApi());
            Assert.assertEquals(cafInfo.getImplementAssembly(), rpcResource.getClassName());
            Assert.assertEquals(cafInfo.getServiceUnit(), rpcResource.getServiceUnit());
        } else if (sourceInfo.getSourceType() == SgIApiSourceInfo.SOURCE_TYPE) {
            if (resource.getKind() != IApiResource.KIND) {
                return false;
            }
            SgIApiSourceInfo sgInfo = (SgIApiSourceInfo) sourceInfo;
            IApiResource apiResource = (IApiResource) resource;
            Assert.assertEquals(sgInfo.getApiCode(), apiResource.getApiCode());
            Assert.assertEquals(sgInfo.getApiId(), apiResource.getApiId());
            Assert.assertEquals(sgInfo.getApiName(), apiResource.getApiName());
            Assert.assertEquals(sgInfo.getApiSu(), apiResource.getApiServiceUnit());
            Assert.assertEquals(sgInfo.getApiUri(), apiResource.getApiUri());
        } else {
            return false;
        }
        return true;
    }

    @Test
    public void newDataSerializeTest() {
        WebServiceComponent component = ComponentInit.get();
        WebServiceCmpSerializer serializer = new WebServiceCmpSerializer();
        JsonNode compNode1 = serializer.Serialize(component);
        WebServiceComponent component2 = (WebServiceComponent) serializer.DeSerialize(compNode1);
        JsonNode compNode2 = serializer.Serialize(component2);

        Assert.assertEquals(compNode2.toString(), compNode1.toString());
        Assert.assertTrue(CompareUtil.equals(component, component2));
    }
}
